package ss.pku.meeting.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import lava.rt.expcption.PersistenceExecption;
import lava.rt.persistence.sql.SqlDataContextExecption;
import ss.pku.meeting.dto.MeetingEvent;
import ss.pku.meeting.dto.MeetingResource;
import ss.pku.meeting.entities.Msg;
import ss.pku.meeting.service.MeetingCalendarService;

@RestController()
public class MettingCalendarController {

	@Autowired
	private MeetingCalendarService meetingCalendarService;
	
	private static SimpleDateFormat sdf_ymd=new SimpleDateFormat("yyyy-MM-dd");
	
	@GetMapping("/calendar/events")
	public Object getEvents(String start, String end) {
		List events=null;
		try {
			Date startAt = sdf_ymd.parse(start);
			Date endAt=sdf_ymd.parse(end);
			Calendar endCal=Calendar.getInstance();
			endCal.setTime(endAt);
			endCal.set(Calendar.HOUR, 23);
			endCal.set(Calendar.MINUTE, 59);
			endCal.set(Calendar.SECOND,59);
			events= meetingCalendarService.getEvents(startAt,endCal.getTime());
		} catch (Exception e) {
			return Msg.fault().setError(e.getMessage());
		}
		
		return Msg.success().setData(events);
	}
	
	
	@GetMapping("/calendar/event/{id}")
	public Object getEvent(@PathVariable String id ) {
		MeetingEvent ret=null;
		try {
			ret=meetingCalendarService.getEventById(id);
		} catch (SqlDataContextExecption e) {
			// TODO Auto-generated catch block
			return Msg.fault().setError(e.getMessage());
		}
		return Msg.success().setData(ret);
	}
	
	@PostMapping("/calendar/event")
	public Object createEvent(@RequestBody MeetingEvent body) {
		String id=UUID.randomUUID().toString();
		try {
			
			body.setId(id);
			meetingCalendarService.createEvent(body);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return Msg.fault().setError(e.getMessage());
		}
		return Msg.success().setData(id);
	}
	
	@PutMapping("/calendar/event/{id}")
	public Object updateEvent(@PathVariable String id,@RequestBody MeetingEvent body) {
		try {
			body.setId(id);
			meetingCalendarService.updateEvent(body);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return Msg.fault().setError(e.getMessage());
		}
		return Msg.success();
	}
	
	@DeleteMapping("/calendar/event/{id}")
	public Object deleteEvent(@PathVariable String id) {
		try {
			meetingCalendarService.deleteEvent(id);
		} catch (SqlDataContextExecption e) {
			// TODO Auto-generated catch block
			return Msg.fault().setError(e.getMessage());
		}
		return Msg.success();
	}
	
	
	@GetMapping("/calendar/resources")
	public Object getResources() {
		List<MeetingResource> rets=null;
		try {
			rets=meetingCalendarService.getEffectiveResources();
		} catch (PersistenceExecption e) {
			// TODO Auto-generated catch block
			return Msg.fault().setError(e.getMessage());
		}
		return Msg.success().setData(rets);
	}
}
